//! \file Behavior_TODO.cpp
//! \brief Implements the <code>fullsail_ai::fundamentals::Behavior</code> abstract class.
//! \author Jeremiah Blanchard with code from Cromwell D. Enage

#include <queue>
#include <stack>
#include "Behavior.h"
#include "../Structures/QueueList.h"

namespace fullsail_ai { namespace fundamentals {

	unsigned Behavior::createdCount = 0;
	unsigned Behavior::destroyedCount = 0;

	unsigned Behavior::getCreatedCount()
	{
		return createdCount;
	}

	unsigned Behavior::getDestroyedCount()
	{
		return destroyedCount;
	}

	Behavior::Behavior(char const* _description)
	{
		description = _description;
	}

	char const* Behavior::toString() const
	{
		return description;
	}

	bool Behavior::isLeaf() const
	{
		return false;
	}

	//! \todo
	//!   - Implement the <code>Behavior</code> methods.
	size_t Behavior::getChildCount() const
	{
		return children.size();
	}

	Behavior* Behavior::getChild(size_t index)
	{
		return children[index];
	}

	Behavior const* Behavior::getChild(size_t index) const
	{
		return children[index];
	}

	void Behavior::addChild(Behavior* child)
	{
		child->parent = this;
		children.push_back(child);
	}

	// traversals
	void Behavior::breadthFirstTraverse(void (*dataFunction)(Behavior const*)) const
	{
		// starts at root and goes to all nodes at each depth level before moving down to the next depth.
		QueueList<Behavior const*> nodeList;
		nodeList.enqueue(this);
		while(!nodeList.isEmpty())
		{
			Behavior const* node = nodeList.getFront();
			dataFunction(node);
			nodeList.dequeue();

			for(unsigned int i = 0; i < node->getChildCount();++i)
			{
				nodeList.enqueue(node->getChild(i));
			}
		}
	}

	void Behavior::preOrderTraverse(void (*dataFunction)(Behavior const*)) const
	{
		preorderTraverseHelper(this,dataFunction);
	}

	void Behavior::postOrderTraverse(void (*dataFunction)(Behavior const*)) const
	{
		postorderTraverseHelper(this,dataFunction);
	}

	// helper functions
	void Behavior::preorderTraverseHelper(Behavior const* node , void (*dataFunction)(Behavior const*))const
	{
		dataFunction(node);
		for(unsigned int i = 0; i < node->getChildCount();++i)
		{
			preorderTraverseHelper(node->getChild(i),dataFunction);
		}
	}

	void Behavior::postorderTraverseHelper(Behavior const* node ,void (*dataFunction)(Behavior const*))const
	{
		for(unsigned int i = 0; i < node->getChildCount();++i)
		{
			postorderTraverseHelper(node->getChild(i),dataFunction);
		}
		dataFunction(node);
	}




}}  // namespace fullsail_ai::fundamentals
